package com.lbkj.ops.controller;

import com.lbkj.ops.common.exception.Http404Exception;
import com.lbkj.ops.common.util.CuxBeanUtils;
import com.lbkj.ops.dao.OpsServerUserDao;
import com.lbkj.ops.model.OpsServerUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/bpi/server-users")
public class ServerUserController {

    @Autowired
    private OpsServerUserDao dao;

    @GetMapping
    public List<OpsServerUser> list(@RequestParam Long serverId) {
        return dao.findAllByServerId(serverId);
    }

    @PostMapping
    public OpsServerUser save(@RequestBody OpsServerUser data) {
        Long id = data.getId();
        if(id != null) {
            OpsServerUser raw = dao.findById(data.getId()).orElseThrow(Http404Exception::new);
            CuxBeanUtils.copyPropertiesIgnoreNull(data, raw);
            dao.save(raw);
        } else {
            dao.save(data);
        }
        return data;
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id) {
        OpsServerUser raw = dao.findById(id).orElseThrow(Http404Exception::new);
        dao.delete(raw);
    }

}
